//
//  JJTabBarController.swift
//  微博1
//
//  Created by 卷卷 on 15/9/1.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

class JJTabBarController: UITabBarController {

    //是苹果专为‘代码创建视图’层次结构设计的函数，可以和XIB／ Storyboard等价
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //添加子控制器并不会创建 tab 的按钮
        addChildViewControllers()
        
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //添加加号button
        setupComposeButton()
    }
    
    //单击撰写微博按钮，add Target的方法数通过消息转发调用的，不能设置为private
   @objc private func clickedComposeButton()
    {
       print(__FUNCTION__)
        
        //点击加号切换到composeViewCotroller
        let vc:UIViewController
        
        if JJUserAccountViewModel.shareUserAccount.userLogon{
            
            vc = JJComposeController()
        }else{
            
            vc = JJOAuthViewController()
        }
        
        let nav = UINavigationController(rootViewController: vc)
        
       presentViewController(nav, animated:true, completion: nil)
    }
    
    //设置加号按钮
    func setupComposeButton()
    {
        let w = view.bounds.width / CGFloat(childViewControllers.count)
        
        let rect = CGRect(origin: CGPointZero, size:CGSize(width: w, height: tabBar.bounds.height))
        
        
        coposedButton.frame = CGRectOffset(rect, 2 * w, 0)
        
    }

    
    //添加子控制器
    private func addChildViewControllers(){
        
        // 设置tintColor
        tabBar.tintColor = UIColor.orangeColor()
        
        //添加控制器
        addChildViewController(JJHomeTableViewController(), title: "首页", imageName:"tabbar_home")
        addChildViewController(JJMessageController(), title: "消息", imageName: "tabbar_message_center")
        // 添加空控制器
        addChildViewController(UIViewController())
        addChildViewController(JJDiscoveryController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(JJProfileController(), title: "我", imageName: "tabbar_profile")

    }

    /**
    *  添加指定的控制器
    
    vc ：控制器
    title ：标题
    image Name：图像名称
    */
    
    private func addChildViewController(vc: UIViewController ,title:String ,imageName:String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    //MARK: - 懒加载加号按钮
    //撰写按钮
    lazy var coposedButton :UIButton = {
        
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:
            UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "clickedComposeButton", forControlEvents: UIControlEvents.TouchUpInside)

        self.tabBar.addSubview(button)
        
        return button
    }()
    
}
